var app = angular.module('myApp', []);

app.controller('AdminUserListController', function($rootScope,$scope,$http,$location) {
	$scope.basePath = globalPath.getBasePath();
	
    var users = [];

    $scope.users = users;

    $scope.init = function() {
        $http({
            method : 'GET',
            url : $scope.basePath + 'admin/back_user',
            params : {

            },
        }).
        success(function(response) {
            console.log("success");
            console.log(response);
            users = response.data;
            $scope.users = users;
        }).
        error(function(response) {
            console.log("error");
            console.log(response);
        });
    }

    $scope.init();

    $scope.refresh = function() {
    	$scope.searchUsername = '';
        $scope.init();
    }

    $scope.delUser = function(userId, username) {
        var cfm = confirm("确认删除" + username + "?");
        if (!cfm) {
            return ;
        }

        $http({
            method : 'POST',
            url : $scope.basePath + 'user/back_delete',
            data : {
                userId : userId
            },
            headers:{'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'},
            transformRequest: function(obj) {
                var str = [];
                for(var p in obj){
                    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                }
                console.log(str.join("*"));
                return str.join("&");
            }
        }).
        success(function(response) {
            console.log("success");
            console.log(response);
            alert(response.msg);
            $scope.init();

        }).
        error(function(response) {
            console.log("error");
            console.log(response);
        });

    }

    $scope.updUser = function(userId) {

        alert(userId + "updUser()");
    }

    $scope.search = function() {

    	var searchUsername = $scope.searchUsername;
        // 方法一：本地缓存查询

        // 方法二：请求数据库
        console.log(searchUsername);

        $http({
            method : 'GET',
            url : $scope.basePath + 'admin/back_user',
            params : {
                username : searchUsername
            },
        }).
        success(function(response) {
            console.log("success");
            console.log(response);
            users = response.data;
            $scope.users = users;
        }).
        error(function(response) {
            console.log("error");
            console.log(response);
        });

    }
}).filter('userType', function() {
    return function(val) {
        switch (val) {
            case 1:
                return '管理员';
            case 2:
                return '普通用户';
            default:
                return '无此身份';
        }
    }
}).filter('genderFilter', function() {
    return function(val) {
        switch (val) {
            case 1:
                return '男';
            case 2:
                return '女';
            default:
                return '无此性别';
        }
    }
})